package com.cloud.mkz.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.mkz.core.common.util.ResponseResult;
import com.cloud.mkz.core.log.annotation.SysLog;
import com.cloud.mkz.admin.api.entity.RbacUserRole;
import com.cloud.mkz.admin.service.RbacUserRoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 用户权限关系
 *
 * @author mkz code generator
 * @date 2021-03-21 17:46:26
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/rbacuserrole" )
@Api(value = "rbacuserrole", tags = "用户权限关系管理")
public class RbacUserRoleController {

    private final  RbacUserRoleService rbacUserRoleService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param rbacUserRole 用户权限关系
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('admin_rbacuserrole_get')" )
    public ResponseResult getRbacUserRolePage(Page page, RbacUserRole rbacUserRole) {
        return ResponseResult.ok(rbacUserRoleService.page(page, Wrappers.query(rbacUserRole)));
    }


    /**
     * 通过id查询用户权限关系
     * @param userId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{userId}" )
    @PreAuthorize("@pms.hasPermission('admin_rbacuserrole_get')" )
    public ResponseResult getById(@PathVariable("userId" ) Long userId) {
        return ResponseResult.ok(rbacUserRoleService.getById(userId));
    }

    /**
     * 新增用户权限关系
     * @param rbacUserRole 用户权限关系
     * @return R
     */
    @ApiOperation(value = "新增用户权限关系", notes = "新增用户权限关系")
    @SysLog("新增用户权限关系" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('admin_rbacuserrole_add')" )
    public ResponseResult save(@RequestBody RbacUserRole rbacUserRole) {
        return ResponseResult.ok(rbacUserRoleService.save(rbacUserRole));
    }

    /**
     * 修改用户权限关系
     * @param rbacUserRole 用户权限关系
     * @return R
     */
    @ApiOperation(value = "修改用户权限关系", notes = "修改用户权限关系")
    @SysLog("修改用户权限关系" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin_rbacuserrole_edit')" )
    public ResponseResult updateById(@RequestBody RbacUserRole rbacUserRole) {
        return ResponseResult.ok(rbacUserRoleService.updateById(rbacUserRole));
    }

    /**
     * 通过id删除用户权限关系
     * @param userId id
     * @return R
     */
    @ApiOperation(value = "通过id删除用户权限关系", notes = "通过id删除用户权限关系")
    @SysLog("通过id删除用户权限关系" )
    @DeleteMapping("/{userId}" )
    @PreAuthorize("@pms.hasPermission('admin_rbacuserrole_del')" )
    public ResponseResult removeById(@PathVariable Long userId) {
        return ResponseResult.ok(rbacUserRoleService.removeById(userId));
    }

}
